using System;
using System.ComponentModel;
using System.Windows.Forms;
using AntEater.ComponentModel.BuildElements;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Visualizes a Target as a tree node
    /// </summary>
    internal class TargetExecutionTargetTreeNode : TreeNode
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TargetExecutionTargetTreeNode"/> class.
        /// </summary>
        public TargetExecutionTargetTreeNode()
            : base() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TargetExecutionTargetTreeNode"/> class.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="buildFile">The build file.</param>
        /// <param name="isDefaultTarget">if set to <c>true</c> [is default target].</param>
        public TargetExecutionTargetTreeNode(TargetBuildElement target, BuildFile buildFile, bool isDefaultTarget)
            : this() {

            this._target = target;
            this._target.PropertyChanged += new PropertyChangedEventHandler(TargetPropertyChanged);

            this._buildFile = buildFile;

            // Set node properties
            this.RefreshNode();

            if (isDefaultTarget) {
                this.ImageKey = "TargetDefault";
                this.SelectedImageKey = "TargetDefault";
            } else {
                this.ImageKey = "Target";
                this.SelectedImageKey = "Target";
            }
        }

        private TargetBuildElement _target;

        /// <summary>
        /// Gets the target.
        /// </summary>
        /// <value>The target.</value>
        public TargetBuildElement Target {
            get { return _target; }
        }

        private BuildFile _buildFile;

        public BuildFile BuildFile {
            get { return _buildFile; }
        }

        /// <summary>
        /// Refreshes the node.
        /// </summary>
        private void RefreshNode() {
            this.Name = this.BuildFile.BuildFilePath + ";" + this.Target.Name;
            this.Text = this.Target.Name;
        }

        /// <summary>
        /// Targets the property changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void TargetPropertyChanged(object sender, PropertyChangedEventArgs e) {
            RefreshNode();
        }
    }
}
